#include <iostream>
#include <string>
#include <map>
#include <cstring>

// 1.1
bool hasOnlyUniqueCharacters(std::string str)
{
	std::map<char, int> charCounts;
	for(int i=0; i<str.length(); i++) {
		if(charCounts[str[i]] == 1) return false;
		++charCounts[str[i]];
	}

	return true;
}

// 1.2
void reverseCString(char* str)
{
	char reverse[5];
	std::strcpy(reverse, str);
	int n = std::strlen(str) - 1;
	for(int i=n; i>=0; --i) {
		char c = str[i];
		reverse[i] = str[n-i];
		reverse[n-i] = c;
	}
	
	std::cout << reverse << std::endl;
}

// 1.3
void removeDuplicates(std::string &str)
{
	char cPrev = '\0';
	std::string::iterator it, itDest;
	for(it = itDest = str.begin(); it != str.end()-1; ++it) {
		*itDest = *it;
		if (*it != *(it+1))
			++itDest;
	}
	str.erase(itDest, str.end());
}

// 1.4
// actually checks for palindroms
bool areAnagrams(std::string s1, std::string s2)
{
	if (s1.length() != s2.length())
		return false;
	int max = s1.length() - 1;
	for(int i=0; i<s1.length(); ++i) {
		if (s1[i] != s2[max-i])
			return false;
	}
	
	return true;
}

// 1.5
// actually not in-place
std::string replaceAllSpaces(std::string str)
{
	std::string result;
	std::string::iterator it;
	for(it = str.begin(); it != str.end(); ++it) {
		if (*it == ' ') {
			result.append("%20");
		} else {
			result.push_back(*it);
		}
	}
	return result;
}

// 1.6
// Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, 
// write a method to rotate the image by 90 degrees. Can you do this in place?
#define N 5
void printMatrix(int matrix[N][N])
{
	for(int i=0; i<N; i++) {
		for(int j=0; j<N; j++) {
			std::cout << matrix[i][j] << ",";
		}
		std::cout << std::endl;
	}
}
void rotate(int matrix[N][N])
{
	for(int i=0; i<N; i++) {
		for(int j=0; j<N-i; j++) {
			int temp = matrix[i][j];
			matrix[i][j] = matrix [i+j][N-i-1];
			matrix[i+j][N-i-1] = temp;
			std::cout << i << " " << j << " " << j << " " << N-i-1 << std::endl;
		}
	}
}

int main(int argc, char*argv[])
{
#if 0
//1.1
	bool unique = hasOnlyUniqueCharacters(argv[1]);
	if (unique) 
		std::cout << "Only unique characters" << std::endl;
	else
		std::cout << "No." << std::endl;
#endif

#if 0
//1.2
	char *str = "what";
	reverseCString(str);
	std::cout << str << std::endl;
#endif

#if 0
// 1.3
	std::string str = argv[1];
	removeDuplicates(str);
	std::cout << str << std::endl;
	
#endif

#if 0
// 1.4
	bool anagrams = areAnagrams(argv[1], argv[2]);
	if (anagrams) 
		std::cout << "Anagrams" << std::endl;
	else
		std::cout << "No." << std::endl;
#endif

#if 0
// 1.5
	std::string strNoSpaces = replaceAllSpaces(argv[1]);
	std::cout << strNoSpaces << std::endl;
#endif

#if 0
// 1.6
// Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, 
// write a method to rotate the image by 90 degrees. Can you do this in place?
	int matrix[N][N] = 
	{{ 1, 2, 3, 4, 5 },
	{ 1, 2, 3, 4, 5 },
	{ 1, 2, 3, 4, 5 },
	{ 1, 2, 3, 4, 5 },
	{ 1, 2, 3, 4, 5 }};
	printMatrix(matrix);
	rotate(matrix);
	printMatrix(matrix);
#endif
	
	return 0;
}